﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;

namespace Galaxy.AutoCAD.Databases
{
    public static class TextTool
    {
        #region 获取当前图形中所有文字样式名（定义为database的括展函数）
        public static List<string> GetAllTextStyleName(this Database db)
        {
            List<string> result = new List<string>();
            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                TextStyleTable tst = tran.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                foreach (ObjectId id in tst)
                {
                    TextStyleTableRecord tstr = id.GetObject(OpenMode.ForRead) as TextStyleTableRecord;
                    result.Add(tstr.Name);
                }
                tran.Commit();
            }
            return result;
        }
        #endregion
        #region 添加TextSpecialSymbol的结构，封装文字中的特殊字符
        public struct TextSpecialSymbol
        {
            public static readonly string Degree = @"\U+00B0";
            public static readonly string Tolerance = @"\U+00B1";
            public static readonly string Diameter = @"\U+2205";
            public static readonly string AlmostEqual = @"\U+2248";
            public static readonly string Angle = @"\U+2220";
            public static readonly string Notequal = @"\U+2260";
            public static readonly string Square = @"\U+00B2";
            public static readonly string Cube = @"\U+00B3";
            public static readonly string Overline = @"%%o";
            public static readonly string Underline = @"%%u";
            public static readonly string Alpha = @"\U+03B1";
            public static readonly string Belta = @"\U+03B2";
            public static readonly string Gamma = @"U+03B3";
        }
        #endregion
        #region 创建单行文字
        public static DBText CreateDBText(string textString, Point3d position, double height, double rotation)
        {
            DBText result = new DBText();
            result.TextString = textString;
            result.Position = position;
            result.Height = height;
            result.Rotation = rotation;
            return result;
        }
        #endregion
        #region 设置单行文字属性(文字样式、文字内容)
        public static void SetDBTextProperty(this ObjectId dBTextId, ObjectId textStyleId)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                DBObject dBObj = tran.GetObject(dBTextId, OpenMode.ForWrite);
                if (dBObj is DBText)
                {
                    DBText dBText = (DBText)dBObj;
                    dBText.TextStyleId = textStyleId;
                }
                tran.Commit();
            }
        }
        public static void SetDBTextProperty(this ObjectId dBTextId, string textString)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                DBObject dBObj = tran.GetObject(dBTextId, OpenMode.ForWrite);
                if (dBObj is DBText)
                {
                    DBText dBText = (DBText)dBObj;
                    dBText.TextString = textString;
                }
                tran.Commit();
            }
        }
        #endregion
    }
}
